﻿@{
    ViewBag.Title = "GetPriceByArtist";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{BusinessLayer.WorldArtPrice.Entities.Artist artist = (BusinessLayer.WorldArtPrice.Entities.Artist)ViewBag.Artist; }
<script>
    $(document).ready(function () {
        ViewPriceByArtist(1);
    });

    var TotalPage = 0;
    var PageSize=30;
    function ViewPriceByArtist(PageIndex) {
        $("#divData").html("Loading...");
        $.ajax({
            url: '/Price/GetPriceByArtist',
            type: 'POST',
            data: {
                artistId: @artist.ArtistId,
                pageIndex: PageIndex,
                pageSize: PageSize
            },
            success: function (data) {
                var str = '<table id="rounded-corner"><thead><tr>';
                str += '<th class="rounded-company" scope="col"><input id="chkAll" onclick="javascript:SelectAll(this);" type="checkbox" /></th>';
                str += '<th class="rounded" scope="col">Id</th>';
                str += '<th class="rounded" scope="col">ItemName</th>';
                str += '<th class="rounded" scope="col">AuctionTime</th>';        
                str += '<th class="rounded" scope="col">Height</th>';        
                str += '<th class="rounded" scope="col">Width</th>';        
                str += '<th class="rounded" scope="col">Depth</th>';        
                str += '<th class="rounded" scope="col">Measurement</th>';        
                str += '<th class="rounded" scope="col">PriceInUS</th>';        
                str += '<th class="rounded" scope="col">ItemType</th>';                        
                str += '<th class="rounded" scope="col">Status</th>';
                str += '<th class="rounded-q4" scope="col"></th>';
                str += '</tr>';
                TotalPage = Math.ceil(data.TotalRecord / PageSize);
                if (TotalPage > 1) {
                    $("#divPager1").show();
                    $("#divPager2").show();
                }else {
                    $("#divPager1").hide();
                    $("#divPager2").hide();
                }                
                $.each(data.ListItem, function (i, item) {
                    var auctionTime = item.AuctionTime != null ? item.AuctionTime.substring(item.AuctionTime.indexOf("(") + 1, item.AuctionTime.indexOf(")")) : "";
                    str += "<tr>";
                    str += "<td><input type='checkbox' value='true' refer='chk' onclick='javascript:SelectOne(this)' name='check' itemid='"+ item.ID +"' id='check'></td>";
                    str += "<td>"+item.ID+"</td>";
                    str += "<td>"+item.ItemName+"</td>";
                    str += "<td>"+$.format.date(new Date(parseInt(auctionTime)), 'dd/MM/yyyy hh:mm')+"</td>";
                    str += "<td>"+item.Height+"</td>";
                    str += "<td>"+item.Width+"</td>";
                    str += "<td>"+item.Depth+"</td>";
                    str += "<td>"+item.Measurement+"</td>";
                    str += "<td>"+item.PriceInUS+"</td>";
                    str += "<td>"+item.ItemType+"</td>";                    
                    str += "<td>0</td>";
                    str += "<td><a href='/Price/Edit?id="+ item.ID +"'><img border='0' src='/Content/Images/user_edit.png'></a></td>";
                    str += "</tr>";
                });          
                str +='</thead></table>';
                $("#divData").html(str);
                $("#divTotalRecord").html("Total Item :" + data.TotalRecord);
                $("#divPager1").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: ViewPriceByArtist });
                $("#divPager2").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: ViewPriceByArtist });
            }
        });
    }


</script>

<h2>List Price By Artist @artist.FullName</h2>
<a class="bt_green" href="/Price/Create?artistId=@artist.ArtistId"><span class="bt_green_lft"></span><strong>Create</strong><span class="bt_green_r"></span></a>
<a class="bt_red" href="#" onclick="DeleteSelectedItems(this);"><span class="bt_red_lft"></span><strong>Delete items</strong><span class="bt_red_r"></span></a>
<div id="divTotalRecord"></div>
<div id="divPager1" class="pagination"></div>
<div id="divData"></div>
<div id="divPager2" class="pagination"></div>

<input id="hidPriceId" type="hidden" />
    <script type="text/javascript">
        /*Check, Check All*/
        function SelectAll(spanChk) {

            var hidSelected = $("input[id='hidPriceId']");
            var chkList = $("input[refer='chk']");
            var str = "";

            for (var i = 0; i < chkList.length; i++) {
                var chk = chkList[i];
                chk.checked = spanChk.checked;
                if (chk.checked) {
                    str = str + $(chk).attr("itemid") + "_";
                }
            }
            str = str == "" ? "" : str.substring(0, str.length - 1);
            hidSelected.attr("value", str);
            //alert(hidSelected.val());
        }
        function SelectOne() {
            var hidSelected = $("input[id='hidPriceId']");
            var chkList = $("input[refer='chk']");
            var str = "";

            for (var i = 0; i < chkList.length; i++) {
                var chk = chkList[i];
                if (chk.checked) {
                    str = str + $(chk).attr("itemid") + "_";
                }
            }
            str = str == "" ? "" : str.substring(0, str.length - 1);
            hidSelected.attr("value", str);
        }
        function DeleteSelectedItems() {

            if (confirm('Are you sure that want delete these items?')) {
                var hidSelected = $("input[id='hidPriceId']");
                if (hidSelected.val() != "") {

                    var rows = $("input:checked");
                    rows.each(function () {
                        $(this).parent().parent().remove();
                    });

                    $.ajax({
                        url: '/Price/Delete',
                        type: 'POST',
                        data: {
                            listId: hidSelected.val()
                        },
                        success: function (data) {
                            
                        }
                    });

                }
            }
        }
    </script>
